package com.gaofei.admin.aspect;

import com.gaofei.admin.domain.SystemLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Title: LogAspect
 * @Author gaofee
 * @Package com.gaofei.admin.aspect
 * @Date 2024/03/15 15:44
 * @description: 日志记录的切面
 */
@Aspect //声明aop日志的切面
@Component
public class LogAspect {

    @Pointcut("@annotation(com.gaofei.admin.aspect.Log)")
    public void logger() {
    }

    @Around("logger()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //声明通知或者增强的地方
        //每次访问接口,都要记录日志(访问用户ip地址,访问时间),把日志信息保存到数据库||其他地方
        SystemLog systemLog = new SystemLog();
        systemLog.setExploreTime(new Date());
        systemLog.setIpAddr(request.getRemoteAddr()); //封装ip
        System.out.println(systemLog);
        //保存日志到数据库
        //logService.save(systemLog);
     return pjp.proceed();
    }

}
